package com.kelvin.paopao;

import android.content.Context;
import android.support.multidex.MultiDexApplication;
import com.baidu.mapapi.SDKInitializer;
import com.kelvin.kelvinlib.db.dao.DaoMaster;
import com.kelvin.kelvinlib.db.dao.DaoSession;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.easemob.EaseMobHelper;
import com.kelvin.paopao.injector.components.ApplicationComponent;
import com.kelvin.paopao.injector.components.DaggerApplicationComponent;
import com.kelvin.paopao.injector.modules.ApplicationModule;
import com.kelvin.paopao.rxbus.RxBus;
import org.greenrobot.greendao.database.Database;
import cn.jpush.android.api.JPushInterface;
import cn.sharesdk.framework.ShareSDK;

/**
 * Created by Administrator on 2017/5/5.
 */

public class PaoPaoApplication extends MultiDexApplication {
    private final String DB_NAME = "paopao_db";

    private ApplicationComponent mAppComponent;
    private DaoSession mDaoSession;
    private RxBus mRxBus;

    private static Context sContext;

    @Override
    public void onCreate() {
        super.onCreate();
        sContext = getApplicationContext();
        mRxBus = new RxBus();
        initDatabase();
        initInjector();
        initConfig();
    }

    public static Context getContext() {
        return sContext;
    }

    /**
     * 提供给工具类daosession，也可以用dagger2做
     *
     * @return
     */
    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    /**
     * 暴露给dagger2的方法
     *
     * @return
     */
    public ApplicationComponent getAppComponent() {
        return mAppComponent;
    }

    /**
     * 初始化注射器
     */
    private void initInjector() {
        // 这里不做注入操作，只提供一些全局单例数据
        mAppComponent = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this, mDaoSession, mRxBus))
                .build();
    }

    /**
     * 初始化数据库
     */
    private void initDatabase() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, DB_NAME);
        Database database = helper.getWritableDb();
        mDaoSession = new DaoMaster(database).newSession();
    }

    /**
     * 初始化配置
     */
    private void initConfig() {
        //初始化sharesdk
        ShareSDK.initSDK(this);
        //初始化easemob
        EaseMobHelper.getInstance().init(this, mRxBus);
        //初始化百度地图
        SDKInitializer.initialize(getApplicationContext());

        //初始化retrofit
        RetrofitService.init();
        //初始化jpush
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
    }
}
